//
//  BPWelcomeViewController.swift
//  BPWeiBo
//
//  Created by 扒皮大叔 on 16/4/10.
//  Copyright © 2016年 apple. All rights reserved.

import UIKit
import SnapKit
import SDWebImage
import SVProgressHUD
class BPWelcomeViewController: UIViewController {

    //设置背景图片
    override func loadView() {
        super.loadView()
        
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()

    }
    
    //MARK:在试图显示的时候执行动画,而不是在视图上额东西全部加载完成偶在执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //MARK:执行动画
    private func startAnimation() {
        
        //改变我们的图片的约束
        //执行动画  弹簧动画
        /*
         1.usingSpringWithDamping : 阻尼系数 弹簧系数 0 ~ 1,阻尼系数越小弹簧效果越大
         2. initialSpringVelocity: 加速度  开始的时候的加速度, 值越大 开始的速速就越快
         3. 动画的可选项OC: 按位与 XXX | XXX swift: [xxx,xxx]
         */
        
        self.iconImage.snp_updateConstraints { (make) in
            make.centerY.equalTo(view.snp_centerY).offset(-180)
        }
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
           //提前刷新界面
           self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.namelbl.alpha = 1
                    }, completion: { (_) -> Void in
                        //这种方法不建议使用,为了方便管理,我们使用通知
                        //UIApplication.sharedApplication().keyWindow?.rootViewController = BPMainViewController()
                        NSNotificationCenter.defaultCenter().postNotificationName(notifcationName, object: "Main")
                        SVProgressHUD.show()
                })
        }
    }
    
    //MARK:设置约束
    private func setUpUI() {
        view.addSubview(iconImage)
        view.addSubview(namelbl)
        
        //设置圆角
        iconImage.cornerRadius = 45
        //隐藏name
        namelbl.alpha = 0
        iconImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_centerY).offset(125)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        namelbl.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_centerY).offset(-120)
        }
        
        //加载头像
        //网络提供的数据不能够直接使用,需要进行洗数据
        //let imgUrl = NSURL(string: BPUserInfoViewModel.sharedUserInfoViewModel.userInfo?.avatar_large ?? "")
        iconImage.sd_setImageWithURL(BPUserInfoViewModel.sharedUserInfoViewModel.imgUrl)
        _ = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(BPUserInfoViewModel.sharedUserInfoViewModel.imgUrl?.absoluteString)
        //print(image.scale)
    }
    

    //MARK:懒加载控件
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var namelbl:UILabel = {
        
        let v = UILabel()
        v.text = BPUserInfoViewModel.sharedUserInfoViewModel.userInfo?.name
        v.textColor = UIColor.darkGrayColor()
        v.font = UIFont.systemFontOfSize(15)
        
        //自适应大小
        v.sizeToFit()
        return v
        
    }()

}
